package com.yzy.baselibrary.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.blankj.utilcode.util.BarUtils;

/**
 * description:
 *
 * @author : YinZeYu
 * @date : 2017/6/16  14:28 创建
 */
public class NavigationUtils {

  private NavigationUtils() {
    /* cannot be instantiated */
    throw new UnsupportedOperationException("cannot be instantiated");
  }

  public static int getNavigationBarHeight(Activity context) {
    if (navigationBarIsShow(context)) {
      Resources resources = context.getResources();
      int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
      return resources.getDimensionPixelSize(resourceId);
    } else {
      return 0;
    }
  }

  public static boolean navigationBarIsShow(Activity context) {
    if (context == null) {
      return false;
    } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
      return false;
    } else {
      //直接用布局判断
      Window window = context.getWindow();
      if (window != null && window.getDecorView() != null) {
        View decorView = window.getDecorView();
        if (decorView instanceof ViewGroup && ((ViewGroup) decorView).getChildCount() > 0) {
          View firstView = ((ViewGroup) decorView).getChildAt(0);
          if (firstView.getHeight() > 0) {
            return decorView.getHeight() == firstView.getHeight() + BarUtils.getNavBarHeight();
          }
        }
      }
      //正常判断
      WindowManager windowManager = context.getWindowManager();
      Display d = windowManager.getDefaultDisplay();
      DisplayMetrics realDisplayMetrics = new DisplayMetrics();
      d.getRealMetrics(realDisplayMetrics);
      int realHeight = realDisplayMetrics.heightPixels;
      int realWidth = realDisplayMetrics.widthPixels;
      DisplayMetrics displayMetrics = new DisplayMetrics();
      d.getMetrics(displayMetrics);
      int displayHeight = displayMetrics.heightPixels;
      int displayWidth = displayMetrics.widthPixels;
      return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }
  }

  /**
   * 隐藏虚拟键
   */
  public static void hideNavigation(Activity context) {
    if (context == null) {
      return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      Window window = context.getWindow();
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
          | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      window.getDecorView()
          .setSystemUiVisibility(
              View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                  View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                  View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.setNavigationBarColor(Color.BLACK);
    } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB)) {
      context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
          | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_FULLSCREEN
          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
          | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_IMMERSIVE);
    }
  }

  /**
   * 隐藏虚拟键
   */
  public static void hideNavigation(Dialog dialog) {
    if (dialog == null || dialog.getWindow() == null) {
      return;
    }
    Window window = dialog.getWindow();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      window.getDecorView()
          .setSystemUiVisibility(
              View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                  | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_FULLSCREEN
                  | View.SYSTEM_UI_FLAG_IMMERSIVE);
      window.setNavigationBarColor(Color.BLACK);
    }
  }
}
